﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections.ObjectModel;
using System.IO;

namespace Prism.Core.Audio
{
    /// <summary>
    /// 表示一个音频管理器。
    /// </summary>
    /// <remarks>音频管理器用于管理多个音频。</remarks>
    public class AudioManager
    {
        /// <summary>
        /// 初始化音频管理器。
        /// </summary>
        protected AudioManager()
        {
            this.AudioBox = new Collection<IAudio>();
            this.EnableSingleChannel = false;
            this.PreId = -1;
            this.volume = 1;
        }

        /// <summary>
        /// 初始化音频管理器。
        /// </summary>
        /// <param name="factory">传入音频工厂。</param>
        /// <param name="latency">设定缓冲延迟时间。</param>
        public AudioManager(AudioFactory factory, TimeSpan latency)
            : this()
        {
            this.AudioBox = new Collection<IAudio>();
            factory.AddManager(this);
            this.Latency = latency;
            this.Factory = factory;
        }

        public virtual void Reset()
        {
            foreach (var i in this.AudioBox)
            {
                i.Stop();
                i.Close();
            }

            this.AudioBox.Clear();
            this.PreId = -1;
        }

        public int Group { get; internal set; }

        public TimeSpan Latency { get; protected set; }

        private AudioFactory Factory { get; set; }

        /// <summary>
        /// 获取音频盒子。
        /// </summary>
        public Collection<IAudio> AudioBox { get; private set; }

        #region 验证

        /// <summary>
        /// 验证获取对应Id的音频。
        /// </summary>
        /// <param name="id">音频的Id。</param>
        /// <returns>返回该音频。</returns>
        protected virtual IAudio EnsureGetAudio(int id)
        {
            var q = from i in this.AudioBox
                    where i.Id == id
                    select i;
            if (q.Count() == 1)
            {
                return q.Single();
            }
            else throw new ArgumentNullException();
        }

        /// <summary>
        /// 验证对应Id的音频。
        /// </summary>
        /// <param name="id">音频的Id。</param>
        /// <returns>返回布尔值。</returns>
        protected virtual bool EnsureAudio(int id)
        {
            var q = from i in this.AudioBox
                    where i.Id == id
                    select i;
            if (q.Count() == 1) return true;
            else return false;
        }

        protected int PreId { get; set; }

        public bool EnableSingleChannel { get; set; }

        private void ApplySingleChannel()
        {
            if (this.EnableSingleChannel)
            {
                if (this.PreId != -1)
                {
                    this.EnsureGetAudio(PreId).Stop();
                    this.UnLoad(PreId);
                }
            }
        }

        #endregion

        #region 加载与卸载

        /// <summary>
        /// 加载音频对象。
        /// </summary>
        /// <param name="source"></param>
        /// <param name="id"></param>
        public virtual void Load(Stream source, int id)
        {
            if (source != null)
            {
                var audio = new Audio(source, this.Latency);
                audio.Id = id;
                this.AudioBox.Add(audio);
            }
        }

        /// <summary>
        /// 卸载音频对象。
        /// </summary>
        /// <param name="id"></param>
        public virtual void UnLoad(int id)
        {
            var audio = this.EnsureGetAudio(id);
            this.AudioBox.Remove(audio);
            audio.Close();
        }

        #endregion

        #region 音频制御

        /// <summary>
        /// 异步地播放指定标识的音频对象。
        /// </summary>
        /// <param name="id">要指定的Id。</param>
        public virtual void BeginPlay(int id)
        {
            ApplySingleChannel();

            var audio = this.EnsureGetAudio(id);
            audio.Play(this.Volume);

            this.PreId = id;
        }

        /// <summary>
        /// 异步地循环播放指定标识的音频对象。
        /// </summary>
        /// <param name="id">要指定的Id。</param>
        /// <param name="loop">是否循环。</param>
        public virtual void BeginPlay(int id, bool loop)
        {
            ApplySingleChannel();

            var audio = this.EnsureGetAudio(id);
            audio.IsLoop = loop;
            audio.Play(this.Volume);

            this.PreId = id;
        }

        /// <summary>
        /// 暂停指定标识的音频对象。
        /// </summary>
        /// <param name="id">要指定的Id。</param>
        public virtual void Pause(int id)
        {
            var audio = this.EnsureGetAudio(id);
            audio.Pause();
        }

        /// <summary>
        /// 恢复播放指定标识的音频对象。
        /// </summary>
        /// <param name="id">要指定的Id。</param>
        public virtual void Resume(int id)
        {
            var audio = this.EnsureGetAudio(id);
            if (audio.IsPaused)
            {
                audio.Play(this.Volume);
            }
        }

        /// <summary>
        /// 停止播放制定标识的音频对象。
        /// </summary>
        /// <param name="id">要指定的Id。</param>
        public virtual void Stop(int id)
        {
            var audio = this.EnsureGetAudio(id);
            audio.Stop();
        }


        private float volume;
        /// <summary>
        /// 调整音频音量。
        /// </summary>
        public float Volume
        {
            get { return this.volume; }
            set
            {
                this.volume = value;

                foreach (var i in this.AudioBox)
                {
                    if (i.IsPlaying)
                    {
                        i.ChangeAudioVolume(volume);
                    }
                }

            }
        }

        #endregion

    }
}
